package org.example.controller;

import org.example.commonapi.domain.R;
import org.example.commonapi.domain.onlineCode.Code;
import org.example.commonapi.domain.onlineCode.CodeTopic;
import org.example.commonapi.domain.onlineCode.ProblemSearchCriteria;
import org.example.commonapi.domain.onlineCode.TestGroup;
import org.example.commonapi.dto.onlineCode.TagDTO;
import org.example.service.OnlineCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;


/**
 * 题库管理
 */
@RestController
@RequestMapping("/oj/ProblemsGroup")
public class ProblemsGroupController {
    @Autowired
    private OnlineCodeService onlineCodeService;

    /**
     * 上传题目
     * 上传题目流程第一步
     * 注意，如果要更新题目，只需要将题目的id传入即可
     *
     * @param codeTopics 题目信息
     * @return 返回上传结果
     */
    @PostMapping("/problems/uploadProblems")
    public R<Object> uploadProblems(@RequestBody @Validated CodeTopic codeTopics) {
        codeTopics.setId(null);
        return onlineCodeService.uploadProblems(codeTopics);
    }

    /**
     * 上传测试集
     * <p>
     * 上传题目流程第二步
     * 注意，如果要更新测试集，只需要将测试集的id传入即可
     *
     * @return 测试集ID
     */
    @PostMapping("/problems/uploadTestGroup")
    public R<Object> uploadTestGroup(HttpServletRequest httpRequest) throws IOException {
        MultipartHttpServletRequest params = ((MultipartHttpServletRequest) httpRequest);
        MultipartFile file = ((MultipartHttpServletRequest) httpRequest).getFile("file");
        String type = params.getParameter("type");
        String content = params.getParameter("content");
        String problemId = params.getParameter("problemId");
        if ("file".equals(type)) {
            if (file == null) {
                return R.error("文件不能为空");
            }
        } else {
            if (content == null) {
                return R.error("文件内容不能为空");
            }
        }
        TestGroup testGroup = new TestGroup(type, content, file, problemId);
        return onlineCodeService.uploadTestGroup(testGroup);
    }

    /**
     * 上传代码
     * 注意，如果要更新代码，只需要将代码的id传入即可
     *
     * @param code 代码
     *             上传题目流程第三步
     * @return 返回上传结果，整个流程结束
     */
    @PostMapping("/problems/uploadCode")
    public R<Object> uploadCode(@RequestBody @Validated Code code) {
        return onlineCodeService.uploadCode(code);
    }

    /**
     * 获取题目列表
     * @param criteria 查询条件
     * @return
     */
    @PostMapping("/problems")
    public R<Object> getProblems(@RequestBody ProblemSearchCriteria criteria) {
        return onlineCodeService.getProblemsList(criteria.getPage(), criteria.getSize(), criteria.getTags(), criteria.getDifficulty(),  criteria.getTitle());
    }

    /**
     * 获取题目数量
     *
     * @return 返回题目数量
     */
    @GetMapping("/problems/count")
    public R<Object> getProblemsCount() {
        return onlineCodeService.getProblemsCount();
    }

    /**
     * 获取题目(单个)
     *
     * @param id 题目id
     * @return 返回题目
     */
    @GetMapping("/problems/{id}")
    public R<Object> getProblemsById(@PathVariable String id) {
        return onlineCodeService.getProblemsOne(id);
    }

    /**
     * 禁用题目
     *
     * @param id 题目id
     * @return 返回删除结果
     */
    @DeleteMapping("/problems")
    public R<String> deleteProblems(@RequestParam String id) {
        return onlineCodeService.deleteProblems(id);
    }

    /**
     * 恢复题目
     * 恢复题目可用状态
     */
    @PostMapping("/problems/recover")
    public R<String> recoverProblems(@RequestParam String id) {
        return onlineCodeService.recoverProblems(id);
    }


    /**
     * 创建标签
     *
     * @param tag
     * @return 返回创建结果
     */
    @PostMapping("/createTag")
    public R<Object> createTag(@RequestBody TagDTO tag) {
        return onlineCodeService.createTag(tag.getTag(), tag.getTagType());
    }


    /**
     * 创建标签（数组）
     */
    @PostMapping("/createTagList")
    public R<Object> createTagList(@RequestBody List<TagDTO> tags) {
        for (TagDTO tag : tags) {
            onlineCodeService.createTag(tag.getTag(), tag.getTagType());
        }
        return new R<>(null,200, "success");
    }

    /**
     * 获取标签
     *
     * @return 返回标签
     */
    @GetMapping("/tag")
    public R<Object> getTag() {
        return onlineCodeService.getTag();
    }
}
